package com.itheima.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImp implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private RoleDao roleDao;
    @Override
    public PageResult findBypage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Permission> permissions =null;
        if(queryPageBean.getQueryString()!=null&&queryPageBean.getQueryString().length()>0&&!queryPageBean.getQueryString().equals("")){
            queryPageBean.setQueryString("%"+queryPageBean.getQueryString()+"%");
            permissions=permissionDao.findByPage(queryPageBean.getQueryString());
        }else{
            queryPageBean.setQueryString("%%");
            permissions=permissionDao.findByPage(queryPageBean.getQueryString());
        }
        for (Permission permission : permissions) {
            Integer id = permission.getId();
            //根据权限Id查询关联角色姓名
            Set<String> roleNames = roleDao.findRolesNameByPid(id);
            if(roleNames.size()>=2){

            }
            permission.setRoleMames(roleNames);
        }


        return new PageResult(permissions.getTotal(),permissions.getResult());
    }

    @Override
    public Result checkIsRepKey(String keyword,String id) {
        Permission permission=null;
        if(id==null){
            permission = permissionDao.checkIsRepByKeyWord(keyword);
        }else{

            permission = permissionDao.checkIsRepKey(keyword,Integer.parseInt(id));
        }

        if(permission==null){
            return new Result(true,null);
        }else{
            return new Result(false,"KeyWord重复请重新输入");
        }

    }

    @Override
    public void deit(Permission permission) {
        permissionDao.deit(permission);

    }
     //回显数据
    @Override
    public Permission findPermissionById(Integer id) {
        return permissionDao.findPermissionById(id);
    }

    @Override
    public void addPermission(Permission permission) {
        permissionDao.addPermission(permission);
        Integer pid = permission.getId();
        //查询系统管理员
       Integer rid  =  permissionDao.findIdByRole("ROLE_ADMIN");
        //将角色和系统管理员关联
        permissionDao.addPerConentAndRole(rid,pid);
    }

    @Override
    public Result findPermissionContentById(Integer id) {
        //先删除中间表数据
        Integer permissionContentById = permissionDao.findPermissionContentById(id);
        System.out.println(permissionContentById);
        if(permissionContentById!=null){
             return new Result(false, MessageConstant.DELETE_PERMISSION_FAIL);
        }else{
            permissionDao.deletePermissionById(id);
            return new Result(true, MessageConstant.DELETE_PERMISSION_SUCCESS);
        }

    }
}
